package com.hangwei.paperless.adapter;

import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.hangwei.paperless.R;
import com.hangwei.paperless.model.bean.RecordListBean;
import com.hangwei.paperless.widget.MyTextWatcher;
import com.twelve.all.adapter.MyAdapter;
import com.twelve.all.net.CallBack;
import com.twelve.all.utils.Tools;

import java.util.List;

/**
 * Created by 张龙臣 on 2020/11/15.
 */
public abstract class SubProcessAdapter extends MyAdapter<RecordListBean,BaseViewHolder> {

//    private List<RecordListBean> mList;
    private boolean mEnable = true;

    public SubProcessAdapter( @Nullable List<RecordListBean> data) {
        super(R.layout.item_sub_prosess, data);
    }

    @Override
    protected void convert(BaseViewHolder holder, RecordListBean bean) {
        holder.setIsRecyclable(false);
       CheckBox  mCheckbox = holder.getView(R.id.checkbox);
       TextView  mTvSeq = holder.getView(R.id.tvSeq);
       TextView  mTvDes = holder.getView(R.id.tvDes);
       EditText  mEtPN = holder.getView(R.id.etPN);
       TextView  mTvRepairNum = holder.getView(R.id.tvRepairNum);
       TextView  mTvSubNum = holder.getView(R.id.tvSubNum);
       TextView  mTvState = holder.getView(R.id.tvState);
       ImageView mIvEdit = holder.getView(R.id.ivEdit);
       TextView  mTvSave = holder.getView(R.id.tvSave);
        mCheckbox.setEnabled(mEnable);
        mTvDes.setEnabled(mEnable);
        mIvEdit.setEnabled(mEnable);
        mTvSave.setEnabled(mEnable);

        mCheckbox.setChecked(bean.isChecked);
        mCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                bean.isChecked = isChecked;
            }
        });
       mTvDes.setText(Tools.checkedString(bean.description));
       mEtPN.setText(Tools.checkedString(bean.pn));
       mTvSeq.setText(Tools.checkedString(bean.seq));
       mTvRepairNum.setText(Tools.checkedString(bean.routingSheetNo));
       mTvState.setText(Tools.checkedString(bean.subSheetStateName));
       mTvSubNum.setText(Tools.checkedString(bean.subSheetNo));
        if (bean.isDrsOpen) {
           mTvSave.setVisibility(View.VISIBLE);
           mIvEdit.setVisibility(View.GONE);
        } else {
           mTvSave.setVisibility(View.GONE);
           mIvEdit.setVisibility(View.VISIBLE);
        }
       mIvEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               mEtPN.setEnabled(true);
                bean.isDrsOpen = true;
                mIvEdit.setVisibility(View.GONE);
                mTvSave.setVisibility(View.VISIBLE);
            }
        });
       mTvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                savePn(bean.id, bean.pn, new CallBack<Object>() {
                    @Override
                    public void onCompleted(Object o) {
                        mEtPN.setEnabled(false);
                        mIvEdit.setVisibility(View.VISIBLE);
                        mTvSave.setVisibility(View.GONE);
                        bean.isDrsOpen = false;
                    }

                    @Override
                    public void onError() {
//                        ToastUtil.showCenterToast("保存失败");
                    }
                });
            }
        });
       mEtPN.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                bean.pn = s.toString().trim();
            }
        });
    }

//    @NonNull
//    @Override
//    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
//        View inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_sub_prosess, viewGroup, false);
//        MyHolder holder = new MyHolder(inflate);
//        holder.setEnable(mEnable);
//        return holder;
//    }

//    @Override
//    public void onBindViewHolder(@NonNull final MyHolder holder, int i) {
//        holder.setIsRecyclable(false);
//        final RecordListBean bean = mList.get(i);
//        holder.mTvDes.setText(Tools.checkedString(bean.description));
//        holder.mEtPN.setText(Tools.checkedString(bean.pn));
//        holder.mTvSeq.setText(Tools.checkedString(bean.seq));
//        holder.mTvRepairNum.setText(Tools.checkedString(bean.routingSheetNo));
//        holder.mTvState.setText(Tools.checkedString(bean.subSheetStateName));
//        holder.mTvSubNum.setText(Tools.checkedString(bean.subSheetNo));
//        if (bean.isDrsOpen) {
//            holder.mTvSave.setVisibility(View.VISIBLE);
//            holder.mIvEdit.setVisibility(View.GONE);
//        } else {
//            holder.mTvSave.setVisibility(View.GONE);
//            holder.mIvEdit.setVisibility(View.VISIBLE);
//        }
//        holder.mIvEdit.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                holder.mEtPN.setEnabled(true);
//                bean.isDrsOpen = true;
//                holder.mIvEdit.setVisibility(View.GONE);
//                holder.mTvSave.setVisibility(View.VISIBLE);
//            }
//        });
//        holder.mTvSave.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                savePn(bean.id, bean.pn, new CallBack<Object>() {
//                    @Override
//                    public void onCompleted(Object o) {
//                        holder.mEtPN.setEnabled(false);
//                        holder.mIvEdit.setVisibility(View.VISIBLE);
//                        holder.mTvSave.setVisibility(View.GONE);
//                        bean.isDrsOpen = false;
//                    }
//
//                    @Override
//                    public void onError() {
////                        ToastUtil.showCenterToast("保存失败");
//                    }
//                });
//            }
//        });
//        holder.mEtPN.addTextChangedListener(new MyTextWatcher() {
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                bean.pn = s.toString().trim();
//            }
//        });
//    }

    protected abstract void savePn(String id, String pn, CallBack<Object> callBack);

//    @Override
//    public int getItemCount() {
//        if (mList == null)
//            return 0;
//        return mList.size();
//    }



    public void setEnable(boolean enable) {
        mEnable = enable;
    }

//    static class MyHolder extends BaseViewHolder {
//
//        public MyHolder(@NonNull View itemView) {
//            super(itemView);
//        }
//
//        public void setEnable(boolean enable) {
//
//        }
//    }
}
